//
//  BFHomeNoticesView.m
//  BaofooWallet
//
//  Created by 吴斌 on 16/6/20.
//  Copyright © 2016年 宝付网络（上海）有限公司. All rights reserved.
//

#import "BFHomeNoticesView.h"

@interface BFHomeNoticesView ()<UIScrollViewDelegate>
@property (nonatomic,strong) UIScrollView *noticeScrollView;
@property (nonatomic,strong) UIImageView *redImageView;
@property (nonatomic,strong) NSMutableArray *myNoticeViewArr;
@property (nonatomic,strong) NSMutableArray *myNoticeArr;
@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,assign) BOOL lock;
@property (nonatomic,assign) BOOL flag;
@property (nonatomic,assign) NSInteger currentPage;
@property (nonatomic,assign) NSInteger scrollTopicFlag;
@end


#define PWidth  self.bounds.size.width
#define PHeight  self.bounds.size.height
@implementation BFHomeNoticesView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        [self noticeScrollView];
        [self redImageView];
        self.lock = NO;
    }
    return self;
}

-(NSTimer *)timer{
    if (_timer==nil) {
        _timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    }
    return _timer;
}


-(UIScrollView *)noticeScrollView{
    if (_noticeScrollView==nil) {
        CGFloat width = CGRectGetWidth(self.frame);
        CGFloat height = CGRectGetHeight(self.frame);
        _noticeScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
        _noticeScrollView.pagingEnabled = YES;
        _noticeScrollView.showsHorizontalScrollIndicator = NO;
        _noticeScrollView.showsVerticalScrollIndicator = NO;
        _noticeScrollView.bounces = NO;
        _noticeScrollView.delegate =self;
        _noticeScrollView.scrollEnabled = NO;
        [self addSubview:_noticeScrollView];
        
    }
    return _noticeScrollView;
}
- (UIImageView *)redImageView
{
    if (!_redImageView) {
        CGFloat height = CGRectGetHeight(self.frame);
        UIImage *notice_image1 = [UIImage imageNamed:@"sy_notice1"];
//        UIImage *notice_image2 = [UIImage imageNamed:@"sy_notice2"];
//        UIImage *notice_image3 = [UIImage imageNamed:@"sy_notice3"];
        
        _redImageView = [[UIImageView alloc]initWithFrame:CGRectMake(12.0f, (height-notice_image1.size.height)*0.5, notice_image1.size.width, notice_image1.size.height)];
        _redImageView.clipsToBounds = YES;
        _redImageView.image = notice_image1;
        [self addSubview:_redImageView];
        
        //NSArray *imageArr = @[notice_image1,notice_image2,notice_image3];
        
        
        
       // _redImageView.animationImages = imageArr;
        _redImageView.animationDuration = 1.5;
        [_redImageView startAnimating];
    }
    return _redImageView;
}

-(NSMutableArray *)myNoticeViewArr{
    if (_myNoticeViewArr==nil) {
        _myNoticeViewArr = [NSMutableArray array];
    }
    return _myNoticeViewArr;
}
-(NSMutableArray *)myNoticeArr{
    if (_myNoticeArr==nil) {
        _myNoticeArr = [NSMutableArray array];
    }
    return _myNoticeArr;
}


- (void)setNoticesByTitleArr:(NSArray *)titleArr{
    [_timer invalidate];
    _timer = nil; 
    [self.myNoticeArr removeAllObjects];
    [self.myNoticeArr addObjectsFromArray:titleArr];
    NSInteger count = self.myNoticeViewArr.count;
    NSInteger picCount  = titleArr.count;
    CGFloat titleLabelOriginX = self.redImageView.frame.size.width+self.redImageView.frame.origin.x+5.0f;
    if (picCount>count-2) {
        for (int i = 0; i<titleArr.count-(count-2); i++) {
            UILabel *noticeLabel = [[UILabel alloc]init];
            noticeLabel.userInteractionEnabled = YES;
            [self.myNoticeViewArr addObject:noticeLabel];
            [self.noticeScrollView addSubview:noticeLabel];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapPicAction:)];
            [noticeLabel addGestureRecognizer:tap];
        }
    }
    for (int i =0; i<titleArr.count+2; i++) {
        UILabel *noticeLabel = self.myNoticeViewArr[i];
        NSString *notice;
        if (i==0) {
            notice =  [titleArr lastObject];
            noticeLabel.tag = titleArr.count-1;
        }else if (i==titleArr.count+1){
            notice = [titleArr firstObject];
            noticeLabel.tag = 0;
        }else{
            notice = titleArr[i-1];
            noticeLabel.tag = i-1;
        }
        noticeLabel.frame = CGRectMake(titleLabelOriginX, i*PHeight, PWidth,PHeight);
        noticeLabel.textColor= [UIColor colorWithHexString:@"#666666"];
        noticeLabel.font= [UIFont systemFontOfSize:13];
        noticeLabel.text = notice;
    }
    self.noticeScrollView.contentSize = CGSizeMake(0, (titleArr.count+2)*PHeight);
    [self.noticeScrollView setContentOffset:CGPointMake(0, PHeight)];
    if (titleArr.count>1) {
        [self starTimer];
    }
}


#pragma mark - scrollView

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView.contentOffset.y == self.frame.size.height) {
        self.flag = YES;
    }
    if (self.flag) {
        if (scrollView.contentOffset.y == 0)
        {
            self.noticeScrollView.contentOffset = CGPointMake(0, self.bounds.size.height * self.myNoticeArr.count);
        }
        else if(scrollView.contentOffset.y == self.bounds.size.height * (self.myNoticeArr.count + 1) )
        {
            self.noticeScrollView.contentOffset = CGPointMake(0, self.bounds.size.height);
        }

    }
    self.currentPage = scrollView.contentOffset.y/self.frame.size.height-1;
    self.scrollTopicFlag = self.currentPage+2==2?2:self.currentPage+2;
}
/**用户将要开始拖拽的时候调用*/
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //停止定时器
    [self.timer setFireDate: [NSDate distantFuture]];
}

/**用户将要停止拖拽的时候调用*/
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    //开始定时器
    if (!self.lock) {
        [self performSelector:@selector(starTimer) withObject:nil afterDelay:2.0f];
        self.lock = YES;
    }
}

- (void)starTimer{
    self.lock = NO;
    [self timer];
}

#pragma mark - 定时器滚动轮播图
-(void)nextPage
{
    [self.noticeScrollView setContentOffset:CGPointMake(0, self.bounds.size.height * self.scrollTopicFlag) animated:YES];
    if (self.scrollTopicFlag > [self.myNoticeArr count]) {
        self.scrollTopicFlag = 1;
    }else {
        self.scrollTopicFlag++;
    }
}


- (void)tapPicAction:(UITapGestureRecognizer *)sender{
    
    if ([self.delegate respondsToSelector:@selector(tapNoticeTag:)]) {
        [self.delegate tapNoticeTag:sender.view.tag];
    }
}

-(void)dealloc{
    [self.timer invalidate];
}

@end
